package com.xindaoapp.happypet.viewpager.windows;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.FansAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Fans;
import com.xindaoapp.happypet.bean.Fans.Fan;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.viewpager.ViewPagerWindow;

/**
 * 引用好友，我的关注，我的粉丝
 * 
 * @author xindaoapp
 * 
 */
@Deprecated
public class MyAttentionWindow extends ViewPagerWindow {
    private final int mTabIndex;

    public MyAttentionWindow(Activity context, int tabIndex) {
        super(context);
        mTabIndex = tabIndex;
    }

    @Override
    protected View onCreateView() {
        return View.inflate(mContext, R.layout.layout_listview, null);
    }

    private PullToRefreshListView vPullToRefreshListView;
    private View vLoadingView;

    @Override
    protected void initViews() {
        super.initViews();
        vLoadingView = getView().findViewById(R.id.loading);
        vPullToRefreshListView = (PullToRefreshListView) getView().findViewById(R.id.listview);
        vPullToRefreshListView.setOnRefreshListener(mOnRefreshListener);
        vPullToRefreshListView.setOnItemClickListener(mOnItemClickListener);
    }

    private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fan info = (Fan) parent.getAdapter().getItem(position);
            Intent data = new Intent();
            data.putExtra(Constants.KEY_FANS_USERNAME, info.fusername);
            data.putExtra(Constants.KEY_FANS_USERFACE, info.userface);
            mContext.setResult(Activity.RESULT_OK, data);
            mContext.finish();
        }
    };
    private final OnRefreshListener<ListView> mOnRefreshListener =
            new OnRefreshListener<ListView>() {

                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    onLoadDatas();
                }
            };

    @Override
    protected void onLoadDatas() {
        if (mTabIndex == 1) {// 我的粉丝
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getFollowFans(UserState.getUserUid(), Constants.PAGE_FIRST,
                            Constants.PAGE_SIZE, new IRequest<Fans>() {

                                @Override
                                public void request(Fans t) {
                                    if (null != t) {
                                        vPullToRefreshListView.setAdapter(new FansAdapter(
                                                getBaseContext(), t.array, Constants.PAGE_SIZE,
                                                R.layout.fans_list_item, R.layout.item_loading,
                                                true));
                                    }
                                    vPullToRefreshListView.onRefreshComplete();
                                    vLoadingView.setVisibility(View.GONE);
                                }
                            });
        } else {// 我的关注
            // HappyPetApplication
            // .get()
            // .getMoccaApi()
            // .getFollowList(UserState.getUserUid(), Constants.PAGE_FIRST,
            // Constants.PAGE_SIZE, new IRequest<Fans>() {
            //
            // @Override
            // public void request(Fans t) {
            // if (null != t) {
            // vPullToRefreshListView.setAdapter(new MyAttentionAdapter(
            // getBaseContext(), t.array, Constants.PAGE_SIZE,
            // R.layout.fans_list_item, R.layout.item_loading));
            // }
            // vLoadingView.setVisibility(View.GONE);
            // }
            // });
        }
    }

}
